layui.use(['table', 'admin', 'ax','ztree','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var $ZTree = layui.ztree;
    var laydate = layui.laydate;

    /**
     * 产品编号表管理
     */
    var ProductOrder = {
        tableId: "productOrderTable",
        condition: {
            classification: ""
        }
    };


    laydate.render({
        elem: '#time'
        , type: 'datetime'
        , range: ','
        , format: 'yyyy-MM-dd'
    });


    /**
     * 初始化表格的列
     */
    ProductOrder.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'productName', sort: true, title: '产品大类'},
            {field: 'productModelName', sort: true, title: '产品型号'},
            {field: 'productOrderName', sort: true, title: '产品品号'},
            {field: 'color', sort: true, title: '产品颜色'},
            {field: 'colorNum', sort: true, title: '产品色号'},
            {field: 'material', sort: true, title: '产品材质'},
            {field: 'unitVolume', sort: true, title: '单台体积'},
            {field: 'unitWeight', sort: true, title: '单台毛重'},
            {field: 'timeToMarket', sort: true, title: '上市时间'},
            {field: 'minOrderAmount', sort: true, title: '最低起订量'},
            {field: 'minSellingPrice', sort: true, title: '最低销售价'},
            {field: 'uniformSalesPrice', sort: true, title: '全国统一价格'},
            {field: 'conversionRatio', sort: true, title: '换算比例'},
            {field: 'bothfee', sort: true, title: '两地费'},
            {field: 'remark', sort: true, title: '备注'},
            {field: 'createTime', sort: true, title: '创建时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };


    /**
     * 弹出添加对话框
     */
    ProductOrder.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/productOrder/add';
    };

    /**
     * 导出excel按钮
     */
    ProductOrder.exportExcel = function () {
        var checkRows = table.checkStatus(ProductOrder.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    ProductOrder.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/productOrder/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    ProductOrder.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/productOrder/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(ProductOrder.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    /**
     * 点击查询按钮
     */
    ProductOrder.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        queryData['classification'] = ProductOrder.condition.classification;
        table.reload(ProductOrder.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 选择部分类时
     */
    ProductOrder.onClickDept = function (e, treeId, treeNode) {
        ProductOrder.condition.classification = treeNode.id;
        ProductOrder.search();
    };

    //初始化左侧部门树
    var ztree = new $ZTree("productTree", "/classification/getTree");
    ztree.bindOnClick(ProductOrder.onClickDept);
    ztree.init();



    // 渲染表格
    var tableResult = table.render({
        elem: '#' + ProductOrder.tableId,
        url: Feng.ctxPath + '/productOrder/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: ProductOrder.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        ProductOrder.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        ProductOrder.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        ProductOrder.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + ProductOrder.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            ProductOrder.openEditDlg(data);
        } else if (layEvent === 'delete') {
            ProductOrder.onDeleteItem(data);
        } else if (layEvent === 'ShiDetails'){
            ProductOrder.ShiDetailsItem(data);
        }

    });


    //点击详情
    ProductOrder.ShiDetailsItem = function (data) {
        window.location.href = Feng.ctxPath + '/productOrder/shiIndentImgeEdit?id=' + data.id;
    };








});
